package com.haois.mp.database.module.wxserver.util;

import com.haois.mp.database.module.common.daomain.TempMessage;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by RuiHaoZ on 2018/2/1 22:24
 */
public class TempMsgUtil {

    public static String title_Default = "收到一条消息";

    public static final String title_Remind_Bir = "生日提醒";
    public static String[] temp_Remind_Bir = {"name", "birthday", "age", "veryLike", "content"};
    public static String[] temp_Remind_Bir_Color = {"#000000", "#000000", "#000000", "#000000", "#EE0000"};

    public static String title_Fw = "收到一条转发消息";
    public static String[] temp_Fw = {"title", "content", "sender", "receiver"};

    public static String title_Mass = "收到一条群发消息";
    public static String[] temp_Mass = {"title", "content", "sender", "orgName", "msgType"};

    public static String[] temp_Default = {"keyword1", "keyword2", "keyword3", "keyword4", "keyword5", "keyword6", "keyword7", "keyword8"};
    public static String[] temp_Default_Color = {"#000000", "#000000", "#000000", "#000000", "#000000", "#000000", "#000000", "#000000"};

    public static final String title_Sys = "收到一条系统消息";
    public static String[] temp_Sys = {"title", "sender", "content"};
    public static String[] temp_Sys_Color = {"#030E07", "#030E07", "#55AA66"};

    public static final String title_Sub = "收到一条业务提醒";
    public static String[] temp_Sub = {"openId", "nick", "type", "content"};
    public static String[] temp_Sub_Color = {"#000000", "#000000", "#000000", "#000000"};

    public static final String title_Remind_Pec = "车辆违章变更提醒";
    public static String[] temp_Remind_Pec = {"archiveNo", "vehiclePlate", "handled", "fen", "money", "date", "wzCity", "area", "act"};
    public static String[] temp_Remind_Pec_Color = {"#000000", "#0000EE", "#FF3030", "#000000", "#000000", "#000000", "#000000", "#000000", "#EE0000"};


    public static WxMpTemplateMessage createTemp(TempMessage tempMessage, String[] tempKeyWord, String[] tempColor) {
        WxMpTemplateMessage tm = WxMpTemplateMessage.builder()
                .toUser(tempMessage.getToUser())
                .templateId(tempMessage.getTempId())
                .url(StringUtils.isBlank(tempMessage.getUrl()) ? null : tempMessage.getUrl())
                .build();
        Map mapData = tempMessage.getMapData();
        for (int i = 0; i < tempKeyWord.length; i++) {
            WxMpTemplateData wmt = new WxMpTemplateData(tempKeyWord[i], getString(mapData.get(tempKeyWord[i])), tempColor[i]);
            tm.addData(wmt);
        }
        return tm;
    }


    public static WxMpTemplate getTemplateByTitle(List<WxMpTemplate> wxMpTemplates, String tempTitle)
            throws WxErrorException {
        for (WxMpTemplate wxMpTemplate : wxMpTemplates) {
            if (wxMpTemplate.getTitle().equals(tempTitle)) {
                return wxMpTemplate;
            }
        }
        return null;
    }

    public static String[] getTempKeyWord(String tempTitle) {
        if (title_Fw.equals(tempTitle)) {
            return temp_Fw;
        } else if (title_Mass.equals(tempTitle)) {
            return temp_Mass;
        } else {
            return temp_Default;
        }
    }

    public static String getString(Object o) {
        try {
            return (String) o;
        } catch (Exception e) {
            return "";
        }
    }
}
